//
//  STUploadService.swift
//  Story
//
//  Created by 汤泽川 on 2019/5/9.
//  Copyright © 2019 汤泽川. All rights reserved.
//

import Foundation
class STUploadService {
    @discardableResult
    public func uploadFile(fileData : Data, fileType : String, mimeType : String) -> STResponse<String> {
        let response = STResponse<String>()
        let uploadRequest = STRequest<ResponseModel<Dictionary<String,String> > >()
        uploadRequest.requestPath = "file/upload_file"
        uploadRequest.uploadFile(data: fileData, fileType: fileType, mimeType: mimeType) { (state) in
            switch state {
            case .success(let responseModel):
                if let dict = responseModel.data {
                    let url = dict["url"]
                    if url != nil {
                        response.execute(url, nil)
                        break;
                    }
                }
                response.execute(nil, STError.DecodeError)
            case .fail(let error):
                response.execute(nil, error)
            }
        }
        return response
    }
    @discardableResult
    public func uploadImage(fileData : Data) -> STResponse<String> {
        return uploadFile(fileData: fileData, fileType: ".jpg", mimeType: "image/jpg")
    }
    @discardableResult
    public func uploadTxt(fileData : Data) -> STResponse<String> {
        return uploadFile(fileData: fileData, fileType: ".txt", mimeType: "text/plain")
    }
}
